/* @(#)GSAfficheFileDownAction.java    V0.1
 *
 * Project: NEWGS
 *
 * Modify Information:
 * =============================================================================
 *   Author       Date       Description
 *   ------------ ---------- ---------------------------------------------------
 *   owen         2006-5-29
 *
 * Copyright Notice:
 * =============================================================================
 *    Copyright (c) 2001-2006 Beijing HiTRUST Technology Co., Ltd. 1808 Room, 
 *        Science & Technology Building, No. 9 South Street, Zhong Guan Cun, 
 *        Haidian District, Beijing ,100081, China All rights reserved.
 *
 *    This software is the confidential and proprietary information of
 *        Beijing HiTRUST Technology Co., Ltd. ("Confidential Information").
 *        You shall not disclose such Confidential Information and shall use it
 *        only in accordance with the terms of the license agreement you entered
 *        into with HiTRUST.
 *
 * Warning:
 * =============================================================================
 * 
 */
package com.hitrust.gs.action;

import java.io.FileInputStream;

import javax.servlet.ServletOutputStream;

import com.hitrust.gs.bean.GSAfficheBean;
import com.hitrust.gs.business.GSAfficheProcessor;
import com.hitrust.gs.lib.GSBaseConstants;
import com.hitrust.gs.lib.GSSystemConfigurator;
import com.hitrust.gs.service.GSBaseAction;
import com.hitrust.gs.service.GSBaseForm;
import com.hitrust.gs.service.GSBaseManager;

/**
 * 公告文件下载
 */
public class GSAfficheFileDownAction extends GSBaseAction {
    public String execute(GSBaseForm aForm) throws Exception {
        String tId=aForm.getRequest().getParameter("filename");
        
        GSAfficheBean tGSAfficheBean=GSAfficheProcessor.getOneAffiche(tId);
        
        String tFilename=tGSAfficheBean.getGs_affiche_filename();
        String upLoadPath = GSSystemConfigurator.getParam("gs.upload.savepath");
    	String tPath = "";
    	if(upLoadPath.startsWith(":/", 1)){
    		tPath = GSSystemConfigurator.getParam("gs.upload.savepath");
    	}else{
    		tPath=aForm.getSession().getServletContext().getRealPath(upLoadPath);
    	}	
    	if(tPath.equals("")||tPath == null){
    		tPath = GSSystemConfigurator.getParam("gs.upload.savepath");
    	}
        aForm.getResponse().setContentType( "application/octet-stream;charset=UTF-8");
        aForm.getResponse().setHeader("Content-disposition","attachment; filename=" +GSBaseManager.toUtf8String(tFilename));

        FileInputStream tInputStream = new FileInputStream(tPath+"/"+tFilename);
        
        ServletOutputStream tOutputStream = aForm.getResponse().getOutputStream();
        byte[] buff = new byte[512];
        int readCount = 0;
        readCount = tInputStream.read(buff);
        while (readCount != -1){
            tOutputStream.write(buff, 0, readCount); 
            
           readCount = tInputStream.read(buff);
        }
        tOutputStream.flush();
        if (tInputStream!=null){
            tInputStream.close();
        }
        if (tOutputStream!=null){
            tOutputStream.close();
        }
       
        return GSBaseConstants.FORWARD_SUCCESS;
    }
    
    

}